﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OUSMS.AppLogic.DataAccessLayer;
namespace OUSMS.AppLogic.BusinessLogicLayer
{
    [System.ComponentModel.DataObject(true)]
    public class SecondDepartmentsBLL:IDisposable
    {
        DatabaseContext _DatabaseContext;
        public SecondDepartmentsBLL():this(new DatabaseContext())
        { 
        }

        public SecondDepartmentsBLL(DatabaseContext DatabaseContext)
        {
            _DatabaseContext = DatabaseContext;
        }

        public void Dispose()
        {
            if (_DatabaseContext != null)
            {
                _DatabaseContext.Dispose();
                _DatabaseContext = null;
            }

            GC.SuppressFinalize(this);
        }

        public IQueryable<SecondDepartments> GetSecondDepartmentsByDepartmentID(int deparmentID)
        {
            return _DatabaseContext.SecondDepartments.Where(s => s.DepartmentID.Equals(deparmentID) && !s.IsDeleted);
        }

        public IQueryable<SecondDepartments> GetAllActiveSecondDepartmentsByDepartmentID(int maximumRows, int startRowIndex, out int totalRowCount, string sortByExpression, int deparmentID)
        {
            IQueryable<SecondDepartments> departments = _DatabaseContext.SecondDepartments.Where(d => !d.IsDeleted && d.DepartmentID == deparmentID);
            totalRowCount = departments.Count();
            return departments.OrderBy(d => d.Name).Skip(startRowIndex).Take(maximumRows);
        }

        public void AddSecondDepartment(SecondDepartments secondDepartment)
        {
            _DatabaseContext.SecondDepartments.Add(secondDepartment);
            _DatabaseContext.SaveChanges();
        }

        public SecondDepartments GetSecondDepartmentByID(int secondDepartmentID)
        {
            return _DatabaseContext.SecondDepartments.Where(s => s.ID == secondDepartmentID).FirstOrDefault();
        }

        public void SaveChanges()
        {
            _DatabaseContext.SaveChanges();
        }
    }
}